const jwt = require("jwt-simple");

const jwtEncode = (adminName, secret) => {
  return jwt.encode({
    adminName,
    exp: Date.now() / 1000 + 60 * 60 //过期时间1小时
  }, secret);
}

const jwtDecode = (token, secret) => {
  try {
    const {
      adminName
    } = jwt.decode(token, secret);
    //如果解码成功,则返回一个对象 对象的code状态是1,并还有一个用户名
    return {
      code: 1,
      adminName
    }

  } catch (e) {
    //如果解码失败,则返回一个对象,状态是0
    return {
      code: 0
    }
  }
}

module.exports = {
  jwtEncode,
  jwtDecode
}